//
//  GWHomeViewController.m
//  ShandsMod
//
//  Created by zh_s on 2019/9/27.
//  Copyright © 2019 zh_s. All rights reserved.
//

#import "GWHomeViewController.h"
#import "GWHomeModel.h"
#import "GWHomeListCell.h"
#import "MoreThreadVC.h"
#import "HomeTotalPriceCell.h"
#import "HomeWalletAddressCell.h"
#import "HomeSegmentV.h"
#import "HomeAssetListCell.h"
#import "HomeNetListCell.h"

//vc
#import "ChangeWalletVC.h"
#import "CreateWalletVC.h"
#import "GWCodeLoginViewController.h"

@interface GWHomeViewController ()<UITableViewDelegate,UITableViewDataSource,HomeSegmentVDelegate>
@property (nonatomic, strong) UITableView *mainTableView;
@property (nonatomic, strong) NSMutableArray *dataArr;

@property (nonatomic, strong) GWHomeModel *homeModel;

@end

@implementation GWHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self data_init];

    UIImageView *backV = [[UIImageView alloc] init];
    [self.view addSubview:backV];
    backV.image = IMAGE_NAME(@"bg_home_view");
    backV.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    [self.view sendSubviewToBack:backV];
//    [backV.layer addSublayer:[UIColor getLeftWithRightGradualChangingColorWithViewBounds:backV fromColor:@"FFFFFF" toColor:@"C8D9FE"]];

    UIImageView *screenImg = [[UIImageView alloc] init];
    [self.view addSubview:screenImg];
    screenImg.frame = CGRectMake(125, NAV_HEIGHT + 30, SCREEN_WIDTH - 125, 350);
    screenImg.image = IMAGE_NAME(@"bg_home_screen");

    [self createCustomNavView];

    [self UI_init];

    // Do any additional setup after loading the view.
}


#pragma mark ----- UI_init
- (void)UI_init {
    [self.view addSubview:self.mainTableView];
}
#pragma mark ----- data_init
- (void)data_init {
    self.dataArr = [NSMutableArray arrayWithCapacity:0];
    NSArray *timeArr = @[@"00:00",@"01:00",@"02:00",@"03:00",@"04:00",@"05:00",@"06:00",@"02:00",@"03:00",@"04:00",@"05:00",@"06:00",@"02:00",@"03:00",@"04:00",@"05:00",@"06:00"];
    NSArray *contentArr = @[@"runLoop相关面试",@"",@"多线程给相关面试",@"花几十块",@"",@"哈吉斯看的哈尽快送达",@"06:00",@"02:00",@"03:00",@"04:00",@"05:00",@"06:00",@"02:00",@"03:00",@"04:00",@"05:00",@"06:00"];
    for (NSInteger i = 0; i < timeArr.count; i ++) {
        GWHomeModel *model = [[GWHomeModel alloc] init];
        model.titleStr = timeArr[i];
        model.contentStr = contentArr[i];
        [self.dataArr addObject:model];
    }
    _homeModel = [[GWHomeModel alloc] init];
    _homeModel.segmentState = 0;
}
#pragma mark ----- tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }else if (section == 1) {
        return 1;
    }
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        HomeTotalPriceCell *priceCell = [tableView dequeueReusableCellWithIdentifier:@"priceCell" forIndexPath:indexPath];
     
        return priceCell;
    }else if (indexPath.section == 1) {
        HomeTotalPriceCell *addressCell = [tableView dequeueReusableCellWithIdentifier:@"addressCell" forIndexPath:indexPath];
     
        return addressCell;
    }else {
        if (_homeModel.segmentState == 0) {//资产cell
            HomeAssetListCell *assetListCell = [tableView dequeueReusableCellWithIdentifier:@"assetListCell" forIndexPath:indexPath];
            return assetListCell;
    
        }else {
            
            HomeNetListCell *netListCell = [tableView dequeueReusableCellWithIdentifier:@"netListCell" forIndexPath:indexPath];
            return netListCell;
        }
    }

}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 2) {
        return 54;
    }
    
    return 8;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 2) {
        HomeSegmentV *segmentV = [[HomeSegmentV alloc] init];
        segmentV.frame = CGRectMake(0, 0, SCREEN_WIDTH, 44);
        segmentV.delegate = self;
        segmentV.homeModel = _homeModel;
        segmentV.backgroundColor = UIColor.clearColor;
        return segmentV;
    }
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        ChangeWalletVC * vc = [[ChangeWalletVC alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    
    }else if (indexPath.row == 1) {
        MoreThreadVC * vc = [[MoreThreadVC alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 130;
    }else if (indexPath.section == 1) {
        return 41;
    }else {
        if (_homeModel.segmentState == 0) {
            return 60;
        }else {
            return 80;
        }
    }
    GWHomeModel *model = self.dataArr[indexPath.row];
    CGFloat height = [ToolsClass getStringHeightWithText:model.contentStr font:FONT_NAME(FONT_Regular, 16) viewWidth:SCREEN_WIDTH -66];
    if (IsStrEmpty(model.contentStr)) {
        return 40;
    }
    ///30 是时间lab高度和上下间距
    return height +30;
}
#pragma mark ----- 点击事件
- (void)createWallet {
    CreateWalletVC *vc = [[CreateWalletVC alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)scanBtnClick {
    GWCodeLoginViewController *vc = [[GWCodeLoginViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark ----- 点击事件
- (void)segmentViewChangeStateWithIndex:(NSInteger)index {
    
    _homeModel.segmentState = index - 5;
    [self.mainTableView reloadData];
}

#pragma mark ----- 懒加载
- (UITableView *)mainTableView {
    if (!_mainTableView) {
        _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NAV_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT -NAV_HEIGHT) style:UITableViewStyleGrouped];
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        _mainTableView.showsVerticalScrollIndicator = NO;
        _mainTableView.rowHeight = 40;
        _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainTableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        _mainTableView.backgroundColor = [UIColor clearColor];
        [_mainTableView registerClass:[GWHomeListCell class]
               forCellReuseIdentifier:@"cell"];
        [_mainTableView registerClass:[HomeTotalPriceCell class]
               forCellReuseIdentifier:@"priceCell"];
        [_mainTableView registerClass:[HomeWalletAddressCell class]
               forCellReuseIdentifier:@"addressCell"];
        
        [_mainTableView registerClass:[HomeAssetListCell class]
               forCellReuseIdentifier:@"assetListCell"];
        
        [_mainTableView registerClass:[HomeNetListCell class]
               forCellReuseIdentifier:@"netListCell"];
    }
    return _mainTableView;
}

- (void)createCustomNavView {
    UIView *navHeaderV = [[UIView alloc] init];
    [self.nav addSubview:navHeaderV];
    [navHeaderV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.nav);
    }];
    navHeaderV.backgroundColor = [UIColor clearColor];

    
    UIButton *createBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [navHeaderV addSubview:createBtn];
    [createBtn addTarget:self action:@selector(createWallet) forControlEvents:UIControlEventTouchUpInside];
    //默认是YES高亮效果
    [createBtn setAdjustsImageWhenHighlighted:NO];
    [createBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(27);
        make.centerY.mas_equalTo(self.nav.titleLabel.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(25, 25));
    }];
    [createBtn setBackgroundImage:IMAGE_NAME(@"icon_home_create") forState:UIControlStateNormal];
    
    UILabel *titleLab = [[UILabel alloc] init];
    [navHeaderV addSubview:titleLab];
    titleLab.textColor = MAIN_BACKCOLOR;
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.font = FONT_NAME(FONT_Semibold, 18);
    titleLab.text = @"首页";
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.nav.mas_centerX);
        make.top.mas_equalTo(STATUS_BAR_HEIGHT +10);
        make.left.right.equalTo(self.nav.titleLabel);
    }];
    
    UIButton *scanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [navHeaderV addSubview:scanBtn];
    [scanBtn addTarget:self action:@selector(scanBtnClick) forControlEvents:UIControlEventTouchUpInside];
    //默认是YES高亮效果
    [scanBtn setAdjustsImageWhenHighlighted:NO];
    [scanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.nav.mas_right).mas_offset(-16);
        make.centerY.mas_equalTo(self.nav.titleLabel.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(25, 25));
    }];
    [scanBtn setBackgroundImage:IMAGE_NAME(@"icon_home_scan") forState:UIControlStateNormal];

    self.nav.backgroundColor = UIColor.clearColor;
    self.leftTitle = @"";
}



@end
